Installing R and RStudio

Augustin Luna
26 January, 2016

Research Fellow
Department of Biostatistics and Computational Biology
Dana-Farber Cancer Institute

Topics to be Covered

  • R: Language Basics, Plotting, Getting Help
  • Using the RStudio Editor

What is R?

  • Free, open source
  • Started in 1993
  • Geared towards scientific computing
    • Created by Ross Ihaka and Robert Gentleman (statisticians)
  • Interpreted; similar to Python and MATLAB

Why is R Popular?

  • Free, open source
  • Interactive data analysis
    • Script-driven rather than menu-driven helps reproducibility
  • Flexible and powerful plotting support
  • Excellent package management system

R Package Management System

  • Large and growing collection of statistical analysis methods
  • Simple package installation; dependency management
  • R scripts usually portable to other platforms
  • Package repositories ensure functionality, documentation, and interoperability
  • Vignettes (tutorials) provided as runnable analyses
  • Large diversity of packages for data analysts
    • This presentation was produced with R packages

Extending R and Package Repositories

  • Comprehensive R Archive Network (CRAN)
    • 5,800 R packages (as of June 2014)
    • Many packages call C, C++, Fortran, or Java code for speedups
  • Bioconductor
    • 800+ R packages focused on bioinformatics
    • 50+ packages dedicated to pathway analysis
  • Devtools
    • R package that allows package installation from code repositories

RStudio

  • https://www.rstudio.com/
  • Available for Windows, OSX, and Linux
  • Simplifies common tasks: plotting, package installation, accessing files, viewing variables, etc.
  • Provides code-completion so users do not have to remember whole lists functions and tons parameters

Installing R and RStudio

YouTube Video Guides to Install R and RStudio

RStudio Overview

Editor
Environment
Plot
Console

Table View of Variables

  • Highlighted boxes open a table view of variable contents

Change Current Directory

  • Highlighted boxes open a table view of variable contents

Making a New R Script

First Script: Hello World!

  • cat() prints a simple message in the console
cat("Hello World!")
Hello World!

Running Hello World Script

  • “Run” button runs current line or selected lines
  • “Source” button runs all lines in file

Code Completion

  • Pressing “Enter” will complete the name of the function

Installing Packages

  • CRAN packages can be installed using RStudio or install.packages()

Creating Reports with R

  • R and RStudio simplifies creating PDF and HTML (webpage) reports that can include:
    • Code
    • R code results
    • Formatted text
    • Tables
    • Figures
  • R Markdown files have the extension .Rmd instead of .R
  • Tutorials (vignettes) are commonly written in this format
  • Markdown cheatsheet: https://www.rstudio.com/resources/cheatsheets/

Example RMarkdown (Rmd) Content

  • NOTE: Remove apostrophes before each line when trying the Rmd file
' # Heading 
' ## Sub-heading 
'
' Text
'
' ```{r}
' #R code
' cat("Hello")
' ```

Converting Rmd to HTML

  • Rendering .Rmd to HTML is done with the Knit HTML button in RStudio

Example Rendered .Rmd

Getting Help